package com.sudo.rpc.api.utils;

import com.sudo.rpc.api.common.Constant;

import java.io.Serializable;

public class RpcRes implements Serializable {
    /**
     *  200：表示成功
     *  500：表示错误，错误信息在msg字段中
     *  501：bean验证错误，不管多少个错误都以map形式返回
     *  502：拦截器拦截到用户token出错
     *  555：异常抛出信息
     *  556: 用户qq校验异常
     *  557: 校验用户是否在CAS登录，用户门票的校验
     */


    // 响应业务状态
    private Integer status;

    // 响应消息
    private String msg;

    // 响应中的数据
    private Object data;


    public RpcRes(Object data) {
        this.status = 200;
        this.msg = "OK";
        this.data = data;
    }

    public RpcRes(Object data, String msg) {
        this.status = 200;
        this.msg = msg;
        this.data = data;
    }

    // 返回查询结果，一般不需要提示信息
    public static RpcRes ok(Object data) {
        return new RpcRes(data);
    }
    public static RpcRes ok(Object data, String msg) {
        return new RpcRes(data,msg);
    }
    // 更新失败，一般需要提示信息
    public static RpcRes errorMsg(String msg) {
        return new RpcRes(500, msg, null);
    }

    // 更新失败，需要数据和提示信息
    public static RpcRes error(Object data, String msg) {
        return new RpcRes(500, msg, data);
    }

    // 更新成功，并且需要提示信息
    public static RpcRes okMsg(String msg) {
        return new RpcRes(200, msg, null);
    }
    // 成功回调
    public static RpcRes okCall(Object data) {
        return new RpcRes(Constant.callCode,null, data);
    }



    public static RpcRes judge(int affectCount , String success, String fail) {
        if (affectCount>0){
            return okMsg(success);
        }else {
            return errorMsg(fail);
        }
    }

    public static RpcRes judge(int affectCount) {
        if (affectCount>0){
            return okMsg("更新成功");
        }else {
            return errorMsg("更新失败");
        }
    }

    public static RpcRes ok() {
        return new RpcRes(null);
    }

    public RpcRes() {

    }

    public RpcRes(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
